//
//  YMCommentView.m
//  YMCommentDemo
//
//  Created by lantaiyuan on 2017/2/24.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "YMCommentView.h"
#import "YMCommentViewCell.h"
#import "YMCommentModel.h"


static NSString *kIndentifier = @"indentifier";

@interface YMCommentView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * flowLayout;
@property (nonatomic, strong) NSArray * titles;
@property (nonatomic, strong) NSArray * icons;
@property (nonatomic, assign) BOOL isVehicle;

@property (nonatomic, strong) UIView * topView;
@property (nonatomic, strong) UIView * functionView;
@property (nonatomic, strong) UIView * vehicleView;
@property (nonatomic, strong) UIButton * cancelBtn;
@property (nonatomic, strong) UILabel * vehicleLabel;

@property (nonatomic, strong) UIView * bottomLine;
@property (nonatomic, strong) UIButton * tempBtn;
@end

@implementation YMCommentView

#pragma mark - Lazy
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, self.functionView.frame.size.width - 40, self.functionView.frame.size.height) collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.bounces = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
    }
    return  _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 40) / 3.0, 106);
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _flowLayout;
}

- (NSArray *)titles{
    return _isVehicle ? @[@"拥挤情况",
                          @"车站设施",
                          @"突发事件",
                          @"整洁情况",
                          @"大型活动"]:@[@"未停靠途经站点",
                                     @"拥挤情况",
                                     @"突发事件",
                                     @"司机服务",
                                     @"整洁情况",
                                     @"车内温度"];
}

- (NSArray *)icons{
    return _isVehicle ? @[@"crowdeddegree",
                          @"facilities",
                          @"breakingthings",
                          @"clean",
                          @"activity"]:@[@"didnotstop",
                                         @"crowdeddegree",
                                         @"breakingthings",
                                         @"service",
                                         @"clean",
                                         @"temperature"];
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        _topView.backgroundColor = [UIColor whiteColor];
        CGFloat btnW = self.frame.size.width / 2;
        CGFloat btnH = 44;
        
        UIView * bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, btnH, 120, 1)];
        bottomLine.backgroundColor = HEX(@"#2ab650");
        
        _bottomLine = bottomLine;
        [_topView addSubview:bottomLine];
        
        [_topView addSubview: [self buttonWithRect:CGRectMake(0, 0, btnW, btnH) fontSize:15 text:@"车辆评价" textColor:HEX(@"#2ab650") tag:0]];
        [_topView addSubview:[self buttonWithRect:CGRectMake(btnW, 0, btnW, btnH) fontSize:15 text:@"站台评价" textColor:HEX(@"#666666") tag:1]];
    }
    return _topView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 44, self.frame.size.width, 44)];
        [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.backgroundColor = HEX(@"#2ab650");
    }
    return _cancelBtn;
}

- (UIView *)vehicleView{
    if (!_vehicleView) {
        _vehicleView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - CGRectGetHeight(self.cancelBtn.frame) - 40, self.frame.size.width, 40)];
        _vehicleView.backgroundColor = [UIColor whiteColor];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
        label.text = @"陕E.1542A";
        label.textColor = HEX(@"#666666");
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = HEX(@"#f5f5f5");
        _vehicleLabel = label;
        [_vehicleView addSubview:label];
    }
    return _vehicleView;
}

- (UIView *)functionView{
    if (!_functionView) {
        CGFloat y = CGRectGetMaxY(self.topView.frame) + 1;
        CGFloat H = self.frame.size.height - 44 - 84 - 1;
        _functionView = [[UIView alloc] initWithFrame:CGRectMake(0, y, self.frame.size.width, H)];
        _functionView.backgroundColor = [UIColor whiteColor];
        [_functionView addSubview:self.collectionView];
    }
    return _functionView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HEX(@"#f5f5f5");
        [self addSubview:self.topView];
        [self addSubview:self.cancelBtn];
        [self addSubview:self.vehicleView];
        [self addSubview:self.functionView];
        
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YMCommentViewCell class]) bundle:nil] forCellWithReuseIdentifier:kIndentifier];
    }
    return self;
}

#pragma mark - Private

- (UIButton *)buttonWithRect:(CGRect)rect
                        fontSize:(CGFloat)fontSize
                        text:(NSString *)text
                   textColor:(UIColor *)textColor
                         tag:(NSInteger)tag
{
    UIButton * btn = [[UIButton alloc] initWithFrame:rect];
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    [btn setTitle:text forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    btn.tag = tag;
    if (tag == 0) {
        _tempBtn = btn;
        _bottomLine.center = CGPointMake(btn.center.x, rect.size.height);
    }
    [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)clickAction:(UIButton *)sender{
    CGPoint center = sender.center;
    center.y = _bottomLine.center.y;
    [UIView animateWithDuration:0.25 animations:^{
        _bottomLine.center = center;
    }];
    _isVehicle = sender.tag;
    
    if (_isVehicle) {
        _vehicleLabel.text = @"茶光 评价";
    }else{
        _vehicleLabel.text = @"陕E.1542A";
    }
    
    [_tempBtn setTitleColor:HEX(@"666666") forState:UIControlStateNormal];
    [sender setTitleColor:HEX(@"#2ab650") forState:UIControlStateNormal];
    
    _tempBtn = sender;
    
    [self.collectionView reloadData];
}

- (void)cancelBtnAction{
    if (_cancelBtnActionBlock) {
        _cancelBtnActionBlock();
    }
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YMCommentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIndentifier forIndexPath:indexPath];
    
    YMCommentModel * commentModel = [[YMCommentModel alloc] init];
    commentModel.image = self.icons[indexPath.item];
    commentModel.title = self.titles[indexPath.item];
    cell.commentModel = commentModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSLog(@"---->%@",self.titles[indexPath.item]);
    
    if (_isVehicle) {
        StationCommentType type = indexPath.item;
        if (_selectStationCommentItemBlock) {
            _selectStationCommentItemBlock(type);
        }
    }else{
        VehicleCommentType type = indexPath.item;
        if (_selectVehicleCommentItemBlock) {
            _selectVehicleCommentItemBlock(type);
        }
    }
}

@end
